package com.flyee.utils
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.Timer;
	/**
	 * SWF库文件加载调用类 
	 * @author lizheng
	 * 
	 */	
	public class ClassLoader extends Loader
	{
		private var m_loadinfo:LoaderInfo;
		private var loadUrl:String = null;
			/**
		 * 连接次数
		 */
		private var progressCounter:int = 0;
		/**
		 * 加载进度时间监听
		 */
		private var progressTimer:Timer = null;
		/**
		 * 时间计数器
		 */
		private var timerCounter:int = 0;
		/**
		 * 连接失败次数计数器
		 */
		private var failedCounter:int = 0;
		/**
		 * 重新次数连接计数器
		 */
		 private var reconnectNum:int = 0;
		/**
		 * 获得加载信息 
		 * @return LoaderInfo
		 * 
		 */		
		public function get loadinfo():LoaderInfo
		{
			return m_loadinfo
		}
		/**
		 * 构造函数 
		 * 
		 */		
		public function ClassLoader() {}
		/**
		 * 加载SWF 
		 * @param url 资源地址
		 * 
		 */		
		public function loadByURL(url:String):void 
		{
			loadUrl = url;
			var context:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
			
			/** 加载到子域 */
			context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
			
			initLoadEvent(this.contentLoaderInfo);
			this.load(new URLRequest(url),context);
		}
		
		/**
		 * 获取当前ApplicationDomain内的类定义
		 * 
		 * @param name 类名称，必须包含完整的命名空间,如 Grave.Function.SWFLoader
		 * @param info 加载swf的LoadInfo，不指定则从当前域获取
		 * @return 获取的类定义，如果不存在返回null
		 */
		public function getClassByName(name:String, info:LoaderInfo = null):Class 
		{
			try 
			{
				if (info == null) 
				{
					return ApplicationDomain.currentDomain.getDefinition(name) as Class;
				}
				return info.applicationDomain.getDefinition(name) as Class;
			} 
			catch (e:ReferenceError) 
			{
				return null;
			}
			return null;
		}
		public function GetClass(name:String, info:LoaderInfo = null):Class 
		{
			try 
			{
				if (info == null) 
				{
					return ApplicationDomain.currentDomain.getDefinition(name) as Class;
				}
				return info.applicationDomain.getDefinition(name) as Class;
			} 
			catch (e:ReferenceError) 
			{
				////MyLog.debug("定义 " + name + " 不存在");
				return null;
			}
			return null;
		}
		private function reload():void
		{
			var request:URLRequest = new URLRequest(this.loadUrl);
			try{
				this.clear();
				this.close();
			}catch(e:IOError){
				throw new Error("can not close this connect!!!");
			}
			try
			{
				
			var context:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
			
			this.unload();
			this.load(request,context);
			}
			catch(error:Error)
			{
				MyLog.debug("Unable to load requested document.");
			}

		}
		/**
		 * @private
		 * 监听加载事件
		 * 
		 * @param info加载对象的LoaderInfo
		 */
		private function initLoadEvent(info : LoaderInfo):void 
		{
			info.addEventListener(ProgressEvent.PROGRESS, this.onProgress);
			info.addEventListener(Event.COMPLETE, this.loaderCompleteHandler);
			info.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
			info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onError);
		}
		
		/**
		 * @private
		 * 移除加载事件
		 * 
		 * @param inft加载对象的LoaderInfo
		 */
		private function removeLoadEvent(info:LoaderInfo):void 
		{
			info.removeEventListener(Event.COMPLETE,loaderCompleteHandler);
			info.removeEventListener(ProgressEvent.PROGRESS,onProgress);
			info.removeEventListener(IOErrorEvent.IO_ERROR,onError);
			info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,onError);
		}
		
		/**
		 * 加载完成事件 
		 * @param e
		 * 
		 */		
		private  function loaderCompleteHandler(e:Event):void 
		{
			var info:LoaderInfo = e.currentTarget as LoaderInfo;
			removeLoadEvent(info);
			m_loadinfo = info;
			clearTimer();
			this.dispatchEvent(e);
		}
		
		/**
		 * 加载进度事件 
		 * @param e
		 * 
		 */		
		private function onProgress(e:ProgressEvent):void 
		{
			progressCounter++;
			this.dispatchEvent(e);
		}
		
		/**
		 * 出错事件 
		 * @param e
		 * 
		 */		
		private function onError(e:Event):void 
		{
			this.dispatchEvent(e);
		}
		private function clearTimer():void{
			reconnectNum = 0;
			progressCounter = 0;
			timerCounter = 0;
		}
		/**
		 *清楚函数 
		 * 
		 */		
		public function clear():void
		{
			if(this.m_loadinfo){
				if(this.m_loadinfo.loader){
					this.m_loadinfo.loader.unload();
				}
				this.m_loadinfo = null;
			}
		}
	}
}